namespace FIREFLYX {
    /** 基础阴影图 着色器*/
    export const BUILTIN_SHADER_BASE_SHADOW_MAP = `ShadowMap`;
    DefaultAsset.shaderGenMap[BUILTIN_SHADER_BASE_SHADOW_MAP] = () => {
        const vsCode = `#version 300 es
        precision mediump float;
        layout (location = 0) in vec4 a_Position; //顶点 位置 是有默认值的 (0,0,0,1)
        layout (location = 4) in vec4 a_Color0;   //顶点 色
        layout (location = 3) in vec2 a_UV;       //UV 纹理坐标

        out vec2 v_uv;
        out vec4 v_color;
        uniform ubo_model_block{
            mat4 u_mMtx;                //模型矩阵
        } uboModel;

        uniform mat4 u_viewProjMtx;

        void main() {
            v_uv = a_UV;
            v_color = a_Color0;
            gl_Position = u_viewProjMtx * uboModel.u_mMtx * vec4(a_Position.xyz,1.0);
        }
        `;

        const fsCode = `#version 300 es
        precision mediump float;

        uniform sampler2D u_mainTex;
        uniform vec4 u_mainColor;           //主颜色 颜色

        uniform vec2 u_resolution;              //画布分辨率

        in vec2 v_uv;
        in vec4 v_color;

        void main(){
            //only depth
            float density = 256.0;
            float minGap = 4.0;
            float a = v_color.a * texture(u_mainTex , v_uv).a * u_mainColor.a;  //获取 alpha 值
            float rate = 1.0 -a;    //进度 0-1
            float w = u_resolution.x;
            float h = u_resolution.y;
            float asp = w / h;
            float gapW = w / density;
            gapW = step(minGap , gapW) * gapW + step(gapW, minGap) * minGap;
            float gapH = gapW * asp;
            float valW = gapW * rate;
            float valH = gapH * rate;
            if(mod(gl_FragCoord.x , gapW) < valW || mod(gl_FragCoord.y , gapH) < valH) discard;     //按alpha 度进行镂空处理
        }
        `;
        //glsl
        const vs = new GLSL();
        vs.setCode(vsCode);
        const fs = new GLSL();
        fs.setCode(fsCode);

        //shader
        let result: Shader = new Shader();
        result.tag = BUILTIN_SHADER_BASE_SHADOW_MAP;
        result.lightMode = RenderPipeline.LM_SHADOW_CASTER;
        result.vertexGLSL = vs;
        result.fragmentGLSL = fs;
        //unifrom 默认值
        result.definePropertyMap = {};

        //apply
        result.apply();

        return result;
    };
}